using System.Collections.Generic;
using KeyCore.Framework.DAL;
using KeyCore.Framework.Model;
using KeyCore.Framework.DAL.Repositories;
using System.Data.Entity.Migrations;

namespace KeyCore.Framework.Migrations
{
    internal sealed class Configuration : DbMigrationsConfiguration<DatabaseContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(DatabaseContext context)
        {

            //if (!WebSecurity.Initialized)
            //{
            //    WebSecurity.InitializeDatabaseConnection("DefaultConnection",
            //                                             "UserProfile",
            //                                             "UserId",
            //                                             "UserName",
            //                                             autoCreateTables: true);
            //}
           // if (!context.Database.Exists())
            {
                var newUsersRoles = new List<UsersRoles>
                                        {
                                            new UsersRoles {RoleName = "administrator"},
                                            new UsersRoles {RoleName = "user"}
                                        };

                UsersRolesRepository usersRolesRepository = new UsersRolesRepository(context);
                // check if role exist if not add
                newUsersRoles.ForEach(x =>
                                          {
                                              if(!usersRolesRepository.RoleExist(x.RoleName))
                                              context.UsersRoles.Add(x);
                                          });
                context.SaveChanges();

                UsersRepository UsersRepository = new UsersRepository(context);

                // create defaul user Admin and add to administrator role
                if(UsersRepository.GetUser("Admin") == null)
                usersRolesRepository.AddUserToRole(
                    UsersRepository.CreateUser("Admin", "keycore_initial", "admin@keycore.pl", true).UserName,"administrator");

            }
        }
    }
}
